<?php

/**
 * Class BackendController 后台控制类
 */
class BackendController extends CoreController
{
    public $layout='//layouts/user/column2';
    public $memberInfo ;
    public $adminPath;
    public function beforeAction($action) {


        if (isset($_POST['sessionId'])) {
            $session = Yii::app()->getSession();
            $session->close();
            $session->sessionID = $_POST['sessionId'];
            $session->open();
        }
        //指定登录页面
        Yii::app()->admin->loginUrl = array('admin/auth/login');
        $assets = Yii::getPathOfAlias('webroot').DIRECTORY_SEPARATOR.'statics'.DIRECTORY_SEPARATOR;
        //在asset中建立一个不冲突文件夹 用来存放JS IMG 以及 CSS  2014 03 10
        $this->staticsPath = Yii::app()->assetManager->publish($assets);
        $apath = Yii::app()->basePath.DS.'modules'.DS.'admin'.DS.'assets'.DIRECTORY_SEPARATOR;
        $this->adminPath = Yii::app()->assetManager->publish($apath);
        if (yii::app()->admin->getState('admin_id') =='')
        {
            //重定向到登录页面
            Yii::app()->admin->loginRequired();
        }else{
            if(Yii::app()->admin->getState('adminSessionTimeout') < time()){
                // timeout 如果过期执行logout方法
                Yii::app()->admin->logout();
                Yii::app()->admin->loginRequired();
            }else{
                yii::app()->admin->setState('adminSessionTimeout', time() + Yii::app()->params['sessionTimeoutSeconds']) ;
            }
        }
        return true;
    }




    /**
     * 设置资源URL
     *
     * @param string $value the base URL that contains all published asset files of backend.
     */
    public function setAssetsUrl($value)
    {
        $this->_assetsUrl=$value;
    }




    /**
     * Gets RestFul data and decodes its JSON request
     * @return mixed
     */
    protected function getInputData()
    {
        return CJSON::decode(file_get_contents('php://input'));
    }

    /**
     * Send raw HTTP response
     * @param int $status HTTP status code
     * @param string $body The body of the HTTP response
     * @param string $contentType Header content-type
     * @return HTTP response
     */
    protected function sendResponse($status = 200, $body = '', $contentType = 'application/json')
    {
        // Set the status
        $statusHeader = 'HTTP/1.1 ' . $status . ' ' . $this->getStatusCodeMessage($status);
        header($statusHeader);
        // Set the content type
        header('Content-type: ' . $contentType);
        echo $body;
        Yii::app()->end();
    }

    /**
     * Return the http status message based on integer status code
     * @param int $status HTTP status code
     * @return string status message
     */
    protected function getStatusCodeMessage($status)
    {
        $codes = array(
            100 => 'Continue',
            101 => 'Switching Protocols',
            200 => 'OK',
            201 => 'Created',
            202 => 'Accepted',
            203 => 'Non-Authoritative Information',
            204 => 'No Content',
            205 => 'Reset Content',
            206 => 'Partial Content',
            300 => 'Multiple Choices',
            301 => 'Moved Permanently',
            302 => 'Found',
            303 => 'See Other',
            304 => 'Not Modified',
            305 => 'Use Proxy',
            306 => '(Unused)',
            307 => 'Temporary Redirect',
            400 => 'Bad Request',
            401 => 'Unauthorized',
            402 => 'Payment Required',
            403 => 'Forbidden',
            404 => 'Not Found',
            405 => 'Method Not Allowed',
            406 => 'Not Acceptable',
            407 => 'Proxy Authentication Required',
            408 => 'Request Timeout',
            409 => 'Conflict',
            410 => 'Gone',
            411 => 'Length Required',
            412 => 'Precondition Failed',
            413 => 'Request Entity Too Large',
            414 => 'Request-URI Too Long',
            415 => 'Unsupported Media Type',
            416 => 'Requested Range Not Satisfiable',
            417 => 'Expectation Failed',
            500 => 'Internal Server Error',
            501 => 'Not Implemented',
            502 => 'Bad Gateway',
            503 => 'Service Unavailable',
            504 => 'Gateway Timeout',
            505 => 'HTTP Version Not Supported',
        );
        return (isset($codes[$status])) ? $codes[$status] : '';
    }




}